package org.genntii.smcommentlike.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.genntii.smcommentlike.client.CommentClient;
import org.genntii.smcommentlike.mapper.CommentLikeMapper;
import org.genntii.smcommentlike.service.CommentLikeService;
import org.genntii.smcommon.constant.ExceptionConstant;
import org.genntii.smcommon.exception.ArticleAlreadyLikeException;
import org.genntii.smcommon.exception.CommentNotFoundException;
import org.genntii.smdomain.entity.CommentLike;
import org.genntii.smdomain.vo.LikeVO;
import org.springframework.stereotype.Service;

@Service
public class CommentLikeServiceImpl extends ServiceImpl<CommentLikeMapper, CommentLike>implements CommentLikeService {

    @Resource
    private CommentLikeMapper commentLikeMapper;

    @Resource
    private CommentClient commentClient;

    @Override
    public LikeVO getCommentLikeVO(Long commentId, Long userId) {
//        if (commentClient.getCommentExists(commentId).getData()){
//            throw new CommentNotFoundException(ExceptionConstant.COMMENT_NOT_FOUND_EXCEPTION);
//        }
        return new LikeVO(commentLikeMapper.getLike(commentId),
                                commentLikeMapper.getDislike(commentId),
                commentLikeMapper.commentLikeExists(userId, commentId) != null?commentLikeMapper.commentLikeExists(userId, commentId):2);
    }

    @Override
    public void commentLike(Long userId, Long commentId, Byte like) {
        CommentLike commentLike = commentLikeMapper.getCommentLiked(userId, commentId);
        if (commentLike != null && commentLike.getLike() == like){
            throw new ArticleAlreadyLikeException(ExceptionConstant.LIKE_EXISTS_EXCEPTION);
        } else if (commentLike != null) {
            if (commentLike.getLike() == 1){
                commentLike.setLike((byte) 0);
            } else if (commentLike.getLike() == 0){
                commentLike.setLike((byte) 1);
            }
            commentLikeMapper.updateById(commentLike);
        }else {
            commentLikeMapper.insert(CommentLike.builder()
                    .commentId(commentId)
                    .like(like)
                    .userId(userId)
                    .build());
        }
    }

    @Override
    public void delCommentLIke(Long commentId, Long userId) {
        if (!commentClient.getCommentExists(commentId).getData()){
            throw new CommentNotFoundException(ExceptionConstant.COMMENT_NOT_FOUND_EXCEPTION);
        }
        CommentLike userCommentLike = commentLikeMapper.getCommentLiked(userId,commentId);
        Byte liked = null;
        if (userCommentLike == null){
            liked = 2;
        } else if (userCommentLike.getLike() == 0) {
            liked = 0;
        } else if (userCommentLike.getLike() == 1){
            liked = 1;
        }
        new LikeVO(commentLikeMapper.getLike(commentId),commentLikeMapper.getDislike(commentId), liked);
    }

}
